﻿using Developer.Core.FrameWork.HelpFile;
using System;
using System.Collections.Generic;
using System.Text;

namespace Developer.Core.FrameWork.Pollys.Options
{
    public class PollyHttpClientOptions
    {
        private static readonly PollyHttpClientOptions options = Appsettings.app<PollyHttpClientOptions>("PollyOptions");
        //1、设置默认值
        public PollyHttpClientOptions()
        {
            //PollyHttpClientOptions options = Appsettings.app<PollyHttpClientOptions>("PollyOptions");
            if (options != null)
                this.SetDefaultValue(options.TimeoutTime, options.RetryCount, options.CircuitBreakerOpenFallCount, options.CircuitBreakerDownTime, options.ResponseMessage);
            else
                this.SetDefaultValue(60, 3, 2, 5, "服务熔断降级了,请稍后重试!");
        }

        public void SetDefaultValue(int TimeOutTime, int RetryCount, int CircuitBreakerOpenFallCount, int CircuitBreakerDownTime, string ResponseMessage)
        {
            this.TimeoutTime = TimeOutTime;
            this.RetryCount = RetryCount;
            this.CircuitBreakerOpenFallCount = CircuitBreakerOpenFallCount;
            this.CircuitBreakerDownTime = CircuitBreakerDownTime;
            this.ResponseMessage = ResponseMessage;
        }

        /// <summary>
        /// 超时时间设置，单位为秒
        /// </summary>
        public int TimeoutTime { set; get; }

        /// <summary>
        /// 失败重试次数
        /// </summary>
        public int RetryCount { set; get; }

        /// <summary>
        /// 执行多少次异常，开启短路器（例：失败2次，开启断路器）
        /// </summary>
        public int CircuitBreakerOpenFallCount { set; get; }

        /// <summary>
        /// 断路器开启的时间(例如：设置为2秒，短路器两秒后自动由开启到关闭)
        /// </summary>
        public int CircuitBreakerDownTime { set; get; }

        /// <summary>
        /// 降级处理(将异常消息封装成为正常消息返回，然后进行响应处理，例如：系统正在繁忙，请稍后处理.....)
        /// </summary>
        public string ResponseMessage { set; get; }
    }
}
